@echo off
echo ===================================================
echo          Nginx Proxy Service Startup
echo ===================================================

set NGINX_PATH=C:\nginx

:: Check if nginx is installed
if not exist "%NGINX_PATH%\nginx.exe" (
    echo Nginx is not installed in %NGINX_PATH% directory
    echo Please download from http://nginx.org/en/download.html
    echo After downloading, extract to C:\nginx directory
    pause
    exit /b 1
)

:: Create conf directory if it doesn't exist
if not exist "%NGINX_PATH%\conf" (
    mkdir "%NGINX_PATH%\conf"
)

:: Create logs directory if it doesn't exist
if not exist "%NGINX_PATH%\logs" (
    mkdir "%NGINX_PATH%\logs"
)

:: Copy configuration file to nginx directory using absolute path
echo Copying configuration file...
copy /Y "%~dp0nginx.conf" "%NGINX_PATH%\conf\nginx.conf"

:: Check if nginx is already running
tasklist /fi "imagename eq nginx.exe" | find ":" > nul
if errorlevel 1 (
    echo Nginx is already running, stopping...
    cd /d "%NGINX_PATH%"
    "%NGINX_PATH%\nginx.exe" -s stop
    timeout /t 2 /nobreak > nul
)

:: Start nginx from its directory with explicit config path
echo Starting Nginx...
cd /d "%NGINX_PATH%"
"%NGINX_PATH%\nginx.exe"

:: Check if nginx started successfully
if %ERRORLEVEL% NEQ 0 (
    echo Failed to start Nginx. Error code: %ERRORLEVEL%
    echo Please check the configuration file and logs.
) else (
    echo.
    echo ===================================================
    echo          Nginx proxy service started
    echo ===================================================
    echo You can now access FinData system at http://localhost
    echo PandaFactor is accessible at http://localhost/factor/
    echo.
    echo To stop Nginx, run: %NGINX_PATH%\nginx.exe -s stop
    echo ===================================================
)

pause 